﻿using System.Windows;
using Fluent;
using GalaSoft.MvvmLight.Messaging;
using MyCalManager.ViewModel;
using MyCalManager.Helper;

namespace MyCalManager
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : RibbonWindow
    {
        public MainWindow()
        {
            InitializeComponent();
            Closing += (s, e) => ViewModelLocator.Cleanup();
            Messenger.Default.Register<string>(
                this,
                m =>
                {
                    if (m == "Close")
                    {
                        this.Close();
                    }
                    if (m == "NODBFILE")
                    {
                        MessageBox.Show("No Database File in \\Data\\ !\n Applicatiion will Close now !", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                        Application.Current.Shutdown(2);
                    }
                });

            var userPrefs = new UserPreferences();

            if (userPrefs.WindowHeight != 0)
            {
                this.Height = userPrefs.WindowHeight;
                this.Width = userPrefs.WindowWidth;
                this.Top = userPrefs.WindowTop;
                this.Left = userPrefs.WindowLeft;
                this.WindowState = userPrefs.WindowState;
            }
            else
                this.WindowStartupLocation = System.Windows.WindowStartupLocation.CenterScreen;
        }

        private void RibbonWindow_Loaded(object sender, RoutedEventArgs e)
        {
            Messenger.Default.Send(true);
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            var userPrefs = new UserPreferences();

            userPrefs.WindowHeight = this.Height;
            userPrefs.WindowWidth = this.Width;
            userPrefs.WindowTop = this.Top;
            userPrefs.WindowLeft = this.Left;
            userPrefs.WindowState = this.WindowState;

            userPrefs.Save();
        }
        
    }
}
